package com.xywy.bloodandroid.act;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.base.BaseActivity;
import com.xywy.bloodandroid.widget.MyViewGroup;
/**
 * *****************************************  
* @Title: SymptomsAct.java 
* @Package com.xywy.bloodandroid.act 
* @Description: TODO(症状表现) 
* @author smz
* @date 2015-1-12 下午6:32:30 
* @version V1.0   
*******************************************
 */
public class SymptomsAct extends BaseActivity {
	private RelativeLayout left_img;
	private ImageView iv_add;
	private EditText edit_tv;
	private TextView cancle;
	private TextView confire;
	private MyViewGroup myGroup;
	private SharedPreferences sp;

	private List<String> list;
	private List<String> list1;
	private String button_text;
	@Override
	protected void initView(Bundle savedInstanceState) {
       setContentView(R.layout.act_symptoms);
       left_img = (RelativeLayout) findViewById(R.id.left_img);
		TextView middle_title = (TextView) findViewById(R.id.middle_title);
		middle_title.setText("症状表现");
		iv_add = (ImageView) findViewById(R.id.iv_add);
		myGroup = (MyViewGroup) findViewById(R.id.myGroup);

	}

	@Override
	protected void initListener() {
		left_img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				SymptomsAct.this.finish();
				
			}
		});
		iv_add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(context, ChanceDiseaseAct.class);
				startActivityForResult(intent, 1);

			}
		});
	}

	@Override
	protected void initData() {
		list = new ArrayList<String>();
		list1 = new ArrayList<String>();

		sp = context.getSharedPreferences("xywy_blood_info",
				Activity.MODE_PRIVATE);
		HashSet<String> set = (HashSet<String>) sp.getStringSet("lsit_symptoms", null);
		if (set != null) {
			Iterator<String> iterator = set.iterator();
			while (iterator.hasNext()) {


				Button btn = new Button(this);
				btn.setBackgroundResource(R.drawable.button_storke);
				btn.setText(iterator.next());
				btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
						LayoutParams.WRAP_CONTENT));
				int count = myGroup.getChildCount();
				myGroup.addView(btn, count - 1);
				myGroup.invalidate();
				btn.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						TextView vv=(TextView) v;
						button_text = vv.getText().toString().trim();
						Finish(button_text);
					}
				});

			}
		}
	}


	@Override
	public void onClick(View v) {
		super.onClick(v);

		TextView vv=(TextView) v;
		button_text = vv.getText().toString().trim();
		Finish(button_text);

	}
	
	private void  Finish(String button_text ){
		Intent date = new Intent();
		date.putExtra("button_text", button_text);
		setResult(2, date);
		finish();
	};

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (data != null) {
			switch (requestCode) {
			case 1:
				String value1;
				value1 = data.getStringExtra("disease");
				if (value1.length() > 0) {
					Button btn = new Button(this);
					btn.setBackgroundResource(R.drawable.button_storke);
					btn.setText(value1);
					btn.setLayoutParams(new LayoutParams(
							LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT));
					int count = myGroup.getChildCount();
					myGroup.addView(btn, count - 1);
					myGroup.invalidate();

					if (value1.length() > 0) {
						list.add(value1);
						Set<String> set = new HashSet<String>();
						set.addAll(list);// 给set填充
						sp.edit().putStringSet("lsit_symptoms", set).commit();
					}
					btn.setOnClickListener(new OnClickListener() {
						
						

						@Override
						public void onClick(View v) {
							TextView vv=(TextView) v;
							button_text = vv.getText().toString().trim();
							Finish(button_text);
						}
					});

					break;

				}
			}
		}
	}
}
